<?php
class classProduct{
	private $_ImageViewer;
	private $_Follow;
	
	function __construct(){
		$this->_ImageViewer = new classImageViewer();
		$this->_Follow 		= new classFollow();		
	}
	
	public function getProducts(){
        //retrieve data from URL
        $_URL 		= new classURL();
        $_PS        = new classProductService();
        $iCatID 	= $_URL->GetValueFromURL($_URL->GetProduct(), 'category');
        $iProdID	= $_URL->GetValueFromURL($_URL->GetProduct(), 'product');
        $iPage      = $_URL->GetValueFromURL($_URL->GetProduct(), 'page');
        $sOrder     = $_URL->GetValueFromURL($_URL->GetProduct(), 'ordering');
		
		
        //show product's list order by category's ID, category have to be real (in DB)
        if(($iCatID>0) and ($iCatID!='') and is_numeric($iCatID) and $_PS->categoryExists($iCatID)){
            $result = self::getProductsList($iCatID, $iPage, $sOrder);
            $_URL->URL_serialize($_URL->GetProduct());
        }
        else{
            if(($iCatID<0) and ($iCatID!='') and is_numeric($iCatID)){ //actions, new-products
                $result = self::getProductsList($iCatID, $iPage, $sOrder);
                $_URL->URL_serialize($_URL->GetProduct());
            }
            else{                 
                //default (page is opened first time)
                $DBResult = mysql_fetch_object(mysql_query('SELECT * FROM first_page'));
                $result = self::getProductsList('-'.$DBResult->page, 0, 'name', '-'.$DBResult->page);
                //$_URL->URL_serialize($_URL->GetProduct());
                //$result = '';
            }
        }
			
        //show detail of product by product's ID
        if(($iProdID>0) and ($iProdID!='') and is_numeric($iProdID) and $_PS->productExists($iProdID)){
			$_Window = new classWindow();
			$_ProductDetail = new classItemDetail();
			$_URL = new classURL();
			$URL_of_Product = $_URL->GetProduct();
			$CloseURL = $_URL->SetURL('',$_URL->GetValueFromURL($URL_of_Product, 'ordering')._URL_VAL_DELIMITER_.$_URL->GetValueFromURL($URL_of_Product, 'category')._URL_VAL_DELIMITER_.'0'._URL_VAL_DELIMITER_.$_URL->GetValueFromURL($URL_of_Product, 'page'), '');
			$result .= $_Window->ShowByURL($_ProductDetail->getProductDetail($iProdID),$CloseURL); 		
		}
		/*	*/	
		return $result;
	}
	/**
     *
     */
	public function getProductsList($iCatID, $iPage, $sOrder, $SetForceCategory=''){
		$_Price = new classPriceExport();            
        $_Paging = new classPaging($SetForceCategory);        

        $Ordering = '';

        switch($sOrder){
            case 'name': $Ordering=' ORDER BY Product_Name '; break;
            case 'dname': $Ordering=' ORDER BY Product_Name DESC '; break;
            case 'price': $Ordering=' ORDER BY Product_Price '; break;
            case 'dprice': $Ordering=' ORDER BY Product_Price DESC '; break;
            default: $Ordering = '';
        }

        //order links
        if(_USER_ORDERING_=='YES')
            $result = $this->getPoductOrderLinks($iCatID);
        else
            $result = '';
        
        //it shows product order by category
        if(is_numeric($iCatID) and $iCatID>=1)
            $result.= $_Paging->showPageNumbers('select count(*) from products where SC_ID='.$iCatID, $iPage, $sOrder);
        //it shows actions
        if($iCatID == -1)
            $result.= $_Paging->showPageNumbers('select count(*) from products, product_actions where (products.ID=product_actions.PA_ProductID)', $iPage, $sOrder);
        //it shows new products
        if($iCatID == -2)
            $result.= $_Paging->showPageNumbers('select count(*) from products, product_news  where (products.ID=product_news .PN_ProductID)', $iPage, $sOrder);
        //it shows products for sale out
        if($iCatID == -3)
            $result.= $_Paging->showPageNumbers('select count(*) from products, product_sale where (products.ID=product_sale.PS_ProductID)', $iPage, $sOrder);

		//table starts
		$result.= '
            <br>
            <br>
			<input type="hidden" id="Show_Product_Image" name="Show_Product_Image" value="">
            <input type="hidden" id="Show_Product_ID" name="Show_Product_ID" value="">
			<input type="hidden" id="Image_Show_As_Child" name="Image_Show_As_Child" value="">
			<input type="hidden" id="Show_Product_Detail_By_ID" name="Show_Product_Detail_By_ID" value="">
			<input type="hidden" name="ProductToBasket_ID" id="ProductToBasket_ID" value="">
			<input type="hidden" name="ProductToBasket_Count" id="ProductToBasket_Count" value="">
			<input type="hidden" name="ProductToBasket_Details" id="ProductToBasket_Details" value="">
			<table class="css_product_mini_main_container">
				<tr>
		';
		//list of products
		$iNo = 0;

        //it shows product order by category
        if(is_numeric($iCatID) and $iCatID>=1)
            $SQL_Result = mysql_query("SELECT ID, Product_Code, Product_Name, Product_Descr FROM products WHERE SC_ID=".$iCatID." ".$Ordering." LIMIT ".$iPage*_ITEMS_COUNT_IN_SINGLE_PAGE_.", "._ITEMS_COUNT_IN_SINGLE_PAGE_);
        //it shows actions
        if($iCatID == -1)
            $SQL_Result = mysql_query('SELECT products.ID as ID, Product_Code, Product_Name, Product_Descr FROM products, product_actions WHERE (products.ID=product_actions.PA_ProductID) '.$Ordering.' LIMIT '.$iPage*_ITEMS_COUNT_IN_SINGLE_PAGE_.', '._ITEMS_COUNT_IN_SINGLE_PAGE_);
        //it shows  new products
        if($iCatID == -2)
            $SQL_Result = mysql_query('SELECT products.ID as ID, Product_Code, Product_Name, Product_Descr FROM products, product_news WHERE (products.ID=product_news.PN_ProductID) '.$Ordering.' LIMIT '.$iPage*_ITEMS_COUNT_IN_SINGLE_PAGE_.', '._ITEMS_COUNT_IN_SINGLE_PAGE_);
        //it shows products for sale out
        if($iCatID == -3)
            $SQL_Result = mysql_query('SELECT products.ID as ID, Product_Code, Product_Name, Product_Descr FROM products, product_sale WHERE (products.ID=product_sale.PS_ProductID) '.$Ordering.' LIMIT '.$iPage*_ITEMS_COUNT_IN_SINGLE_PAGE_.', '._ITEMS_COUNT_IN_SINGLE_PAGE_);

		$SQL_Result_Count = mysql_num_rows($SQL_Result);
		while($sql_array_product = mysql_fetch_object($SQL_Result))
		{
			$iNo++;
			$result .= '
					<td>
			';
			
			//make image's form
			$product_image = self::getImage($sql_array_product->ID);
			//make others data
			$product_short_name 	= self::getLink($iCatID,$sql_array_product->ID, $iPage, $sql_array_product->Product_Name, $sOrder, 'css_product_mini_name_link');
			$product_short_price 	= $_Price->simplePrice($sql_array_product->ID,1);//.'<br>old: '.$sql_array_product->Product_Price.' '._CURRENCY1_;
			$product_short_desc 	= self::getLink($iCatID,$sql_array_product->ID, $iPage, substr($sql_array_product->Product_Descr,0,_TEXTS_LENGHT_IN_PREVIEW_), $sOrder, 'css_product_mini_desccription_link');
			if(_PRODUCT_CODE_SHOWING_=='YES') $product_short_code 	= self::getLink($iCatID,$sql_array_product->ID, $iPage, '['.$sql_array_product->Product_Code.']', $sOrder, 'css_product_mini_name_link');

			//product form from template
			$_Skin = new classTemplate();
			include $_Skin->getActual().'/product_short.inc.php';
				
			$result .= '
					</td>
			';	
			
			//__!!!__!!!__!!!__!!!__!!!__!!!__!!!__!!!__!!!__!!!__!!!__
			//treba testovat porovnavanie / obmedzit prazdne riadky a stlpce na konci / zatial vypada ze funguje...
			if(!($iNo % _PRODUCTS_COUNT_IN_SINGLE_LINE_) and ($iNo < $SQL_Result_Count))
			$result .= '
				</tr>
				<tr>
			';
			//!!!__!!!__!!!__!!!__!!!__!!!__!!!__!!!__!!!__!!!__!!!__!!!
					
		}
		//table ends
		$result .= '
				</tr>
			</table>
            <br>
            <br>
		';

        //if(is_numeric($iCatID) and $iCatID>=1)
        //    $result .= $_Paging->showPageNumbers('select count(*) from products where SC_ID='.$iCatID, $iPage);

        //it shows product order by category
        if(is_numeric($iCatID) and $iCatID>=1)
            $result .= $_Paging->showPageNumbers('select count(*) from products where SC_ID='.$iCatID, $iPage, $sOrder);
        //it show actions
        if($iCatID == -1)
            $result .= $_Paging->showPageNumbers('select count(*) from products, product_actions where (products.ID=product_actions.PA_ProductID)', $iPage, $sOrder);
        //it shows new products
        if($iCatID == -2)
            $result .= $_Paging->showPageNumbers('select count(*) from products, product_news  where (products.ID=product_news .PN_ProductID)', $iPage, $sOrder);
		//it shows products for sale out
        if($iCatID == -3)
            $result .= $_Paging->showPageNumbers('select count(*) from products, product_sale where (products.ID=product_sale.PS_ProductID)', $iPage, $sOrder);

		return $result;
	}

    private function getPoductOrderLinks($iCatID){
        $_URL = new classURL();
        $_Skin= new classTemplate();

        $Arrow_UP = '<img src="'.$_Skin->getActual().'/images/arrow_up.png" class="css_product_ordering_image" alt="'._i18n_product_ASCENDING_.'" >';
        $Arrow_DOWN = '<img src="'.$_Skin->getActual().'/images/arrow_down.png" class="css_product_ordering_image" alt="'._i18n_product_DESCENDING_.'" >';
        $Name_UpLink = '
            <a
                class="css_product_ordering_link"
                rel="nofollow"
                href="'.$_URL->SetURL('','name'._URL_VAL_DELIMITER_.$iCatID._URL_VAL_DELIMITER_.'0'._URL_VAL_DELIMITER_.'0', '').'"
            >'.$Arrow_UP.'</a>
        ';
        $Name_DownLink = '
            <a
                class="css_product_ordering_link"
                rel="nofollow"
                href="'.$_URL->SetURL('','dname'._URL_VAL_DELIMITER_.$iCatID._URL_VAL_DELIMITER_.'0'._URL_VAL_DELIMITER_.'0', '').'"
            >'.$Arrow_DOWN.'</a>
        ';
        $Price_UpLink = '
            <a
                class="css_product_ordering_link"
                rel="nofollow"
                href="'.$_URL->SetURL('','price'._URL_VAL_DELIMITER_.$iCatID._URL_VAL_DELIMITER_.'0'._URL_VAL_DELIMITER_.'0', '').'"
            >'.$Arrow_UP.'</a>
        ';
        $Price_DownLink = '
            <a
                class="css_product_ordering_link"
                rel="nofollow"
                href="'.$_URL->SetURL('','dprice'._URL_VAL_DELIMITER_.$iCatID._URL_VAL_DELIMITER_.'0'._URL_VAL_DELIMITER_.'0', '').'"
            >'.$Arrow_DOWN.'</a>
        ';
        $Name_Caption =_i18n_product_Name_;
        $Price_Caption=_i18n_product_PRICE_;
        include $_Skin->getActual().'/product_order_links.inc.php';

        return $result; //result from included file
    }
	
	private function getLink($iCatID, $iProdID, $iPage, $sText, $sOrder, $sClassCssStyle){
		$_URL 	= new classURL();
		$result ='
			<a class="'.$sClassCssStyle.'" href="'.$_URL->SetURL('',$sOrder._URL_VAL_DELIMITER_.$iCatID._URL_VAL_DELIMITER_.$iProdID._URL_VAL_DELIMITER_.$iPage, '').'"
				'.$this->_Follow->getJS(_i18n_product_SHOW_DETAIL_).'
			>
				'.$sText.'
			</a>
		';	
		return $result;
	}	
	
	private function getImage($imgID){
		if(
			file_exists(_PRODUCTS_IMAGE_DIR_.'/'.$imgID._IMAGES_FILE_TYPE_) and
			file_exists(_PRODUCTS_IMAGE_DIR_.'/'.$imgID._PREVIEW_IMAGE_SUFFIX_._IMAGES_FILE_TYPE_)
		){
            $_pService  = new classProductService;
            $asPInfo    = $_pService->getProductInfo($imgID);

			$result ='
				<a href="'._URL_INACTIVE_ADDR_.'"
					'.$this->_Follow->getJS(_i18n_product_ENLARGE_IMAGE_BY_CLICK_).'
					onmousedown="
                        document.getElementById(\'Show_Product_Image\').value=\''._PRODUCTS_IMAGE_DIR_.'/'.$imgID._IMAGES_FILE_TYPE_.'\';
                        document.getElementById(\'Show_Product_ID\').value=\''.$imgID.'\';
                    "
					onmouseup="classImageViewer();"
				>
					<img
						alt="'.$asPInfo['Product_Name'].'"
						class="css_product_mini_image" 
						src="'._PRODUCTS_IMAGE_DIR_.'/'.$imgID._PREVIEW_IMAGE_SUFFIX_._IMAGES_FILE_TYPE_.'"
					>
				</a>
			';
        }
		else
			$result ='';	
		return $result;
	}
}
?>